/*
 * Trilogy instant messaging system
 * Nathan Reitz, Aaron Mertz, Dave Clark
 * 11/14/2007
*/
package edu.psu.ist.ist311.trilogyIM;

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;

import javax.swing.*;

public class ChatWindow extends JFrame{
	
	public JTextField sendText;		//place to type text
	private JTextArea historyText;		//place to show text
	public JButton confirm;			//button to send
	private AudioClip send, receive;	//audio sounds
    private JScrollPane scrollPane;
	
	public ChatWindow(String recieverName) {
		super("Chat with " + recieverName);

		try{	//send and receive sound files
			send = Applet.newAudioClip(new URL("File:Sounds/send.mid"));
			receive = Applet.newAudioClip(new URL("File:Sounds/receive.mid"));
		}
		catch(Exception exception){
			exception.printStackTrace();
		}
		
		this.createGui();
		
	}

        private void createGui() {
            sendText = new JTextField(30);			//this is for typing stuff in

            historyText = new JTextArea(5, 30);		//this shows the text that has been sent
            historyText.setEditable(false);

            confirm = new JButton("Send");			//adds the button

            scrollPane = new JScrollPane(historyText,	//Makes history scrollable
                            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                

        confirm.setText("Send");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(sendText, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(confirm))
                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(confirm)
                    .addComponent(sendText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        this.pack();
        getContentPane().setSize(350, 175);
    }
	
	public String getMessageText(){
		return sendText.getText();
	}
	
	public void appendText(String message){
		//send.play();		//used to play the sound
		historyText.append(message);	//adds it into history
		System.out.println(this.getBounds());
		sendText.setText("");	//clears the line
		sendText.requestFocus();
	}
	
}